import * as userApi from '@/services/api/user';
import { getLogin, setLogin, logout } from '@/utils/auth';

const user = {
  state: {
    user: '',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    roles: [],
    userInfo: {},
    adminMenus: [],
    menusLoaded: false,
    hasConfirmed: false, // 校验是否有效的账号和密码
    BIND_URL: undefined, // 是否已绑定,为字符串则为未绑定，

  },

  mutations: {
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar;
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles;
    },
    USER_INFO: (state, userInfo) => {
      state.userInfo = userInfo;
    },
    CONFIRMED: (state, hasConfirmed) => {
      state.hasConfirmed = hasConfirmed;
    },
    BIND_URL: (state, bindUrl) => {
      state.BIND_URL = bindUrl;
    },
    [types.SET_ADMIN_MENUS]: (state, menus) => {
      state.adminMenus = menus || [];
    },
    [types.TOGGLE_MENUS_LOADED]: (state, loaded) => {
      state.menusLoaded = loaded;
    },
  },

  actions: {
    // 用户名登录
    async [types.LOGIN]({ commit }, userInfo) {
      const [result, data] = await userApi.login(userInfo);
      if (result !== false) {
        commit('USER_INFO', data);
        setLogin(data);
      }
      return { result, data };
    },
    async [types.LOGIN_CONFIRM]({ commit }, userInfo) {
      const [result, data] = await userApi.loginConfirmApi(userInfo);
      commit('CONFIRMED', result);
      if (result !== false) {
        commit('BIND_URL', data);
      }
      return { result, data };
    },
    async [types.LOGIN_WITH_CODE]({ commit }, userInfo) {
      const [result, data] = await userApi.loginWithCodeApi(userInfo);
      if (result !== false) {
        commit('USER_INFO', data);
        setLogin(data);
      }
      return { result, data };
    },

    // 获取用户信息
    GetUserInfo({ commit, dispatch, state }) {
      return new Promise((resolve) => {
        const userMap = {
          admin: {
            roles: ['admin'],
            token: 'admin',
            introduction: '我是超级管理员',
            avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
            name: 'Super Admin',
          },
          editor: {
            roles: ['editor'],
            token: 'editor',
            introduction: '我是编辑',
            avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
            name: 'Normal Editor',
          },
        };

        const data = Math.random(0, 1) * 10 > 5 ? userMap.admin : userMap.editor;

        commit('SET_ROLES', data.roles);
        commit('SET_AVATAR', data.avatar);

        const { userInfo } = getLogin();

        commit('USER_INFO', userInfo);

        dispatch(types.GET_NOTICE);

        resolve(data);
      });
    },

    // 获取用户菜单列表
    async [types.GET_ADMIN_LIST_MENU]({ commit, state }) {
      commit(types.TOGGLE_MENUS_LOADED, true);
      const [result, data] = await userApi.getAdminListMenu();
      if (result !== false) {
        commit(types.SET_ADMIN_MENUS, data);
      }
      return { result, data };
    },

    // 登出
    [types.LOGOUT]({ commit, state }) {
      return new Promise((resolve) => {
        // commit('SET_TOKEN', '')
        // commit('SET_ROLES', [])
        commit(types.TOGGLE_MENUS_LOADED, false);
        logout();
        resolve();
      });
    },
  },
};

export default user;
